const app = getApp()
const uploadOss = require('../../utils/upload/uploadImg.js')
import { getClassificationList, getClassificationSupplierList, saveFile } from '../../api/index'

Page({
  data: {
    searchHeight: 0,
    cateList: [],
    activeCate: null,
    activeTitle: '',
    childrenList: [],
    merchantList: []
  },
  changeCate ({ currentTarget: { dataset: { item } } }) {
    this.data.activeCate = item.classificationId
    this.data.activeTitle = item.classificationName
    this.data.childrenList = item.childList
    this.setData({
      activeCate: this.data.activeCate,
      activeTitle: this.data.activeTitle,
      childrenList: this.data.childrenList
    })
    this.getMerchantList()
  },
  seeStore ({ currentTarget: { dataset: { item } } }) {
    wx.$nav.navigateTo(`/pages/supplier/index/index?id=${item.supplierId}&name=${item.companyName}`)
  },
  focusInput () {
    wx.$nav.navigateTo('/pages/search/index')
  },
  async chooseMedia () {
    try {
      const { tempFiles } = await wx.$pro.chooseMedia({ count: 1, mediaType: 'image' })
      wx.showLoading({ title: '正在上传图片...' })
      const { fileUrl } = await this.uploadFile(tempFiles[0], 'tempFilePath')
      const { ResultSet } = await saveFile({ fileList: [{ fileUrl: fileUrl }] })
      wx.hideLoading()
      wx.$nav.navigateTo(`/pages/searchImage/index?fileId=${ResultSet.userData.fileIdList[0].fileId}&fileUrl=${ResultSet.userData.fileIdList[0].fileUrl}`)
    } catch (err) {
      wx.hideLoading()
      console.log(err)
    }
  },
  uploadFile (item, key, files = '', index = '', that = '', dataKey = '') {
    return new Promise((resolve, reject) => {
      uploadOss(item[key],
        (res) => {
          resolve(res)
        },
        (err) => {
          resolve()
        },
        files, index, that, dataKey
      )
    })
  },
  selectCate ({ currentTarget: { dataset: { item } } }) {
    wx.$nav.navigateTo(`/pages/cateList/index?first=${this.data.activeCate}&secend=${item.classificationId}&title=${item.classificationName}`)
  },
  async getCateList () {
    try {
      const { ResultSet } = await getClassificationList({})
      if (ResultSet.userData.data.length) {
        this.data.activeCate = ResultSet.userData.data[0].classificationId
        this.data.activeTitle = ResultSet.userData.data[0].classificationName
        this.data.childrenList = ResultSet.userData.data[0].childList
      }
      this.setData({
        activeCate: this.data.activeCate,
        activeTitle: this.data.activeTitle,
        cateList: ResultSet.userData.data,
        childrenList: this.data.childrenList
      })
      this.getMerchantList()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async getMerchantList () {
    try {
      const { ResultSet } = await getClassificationSupplierList({ classificationId: this.data.activeCate })
      this.setData({ merchantList: ResultSet.userData.data })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  onLoad () {
    this.getCateList()
    let query = this.createSelectorQuery();
    query.select('.search-top').boundingClientRect(rect=>{
      let height = rect.height
      this.setData({ searchHeight: height })
    }).exec()
  }
})
